package com.jinlia.show.modules.wxapp.service;

import com.jinlia.show.modules.base.constant.DeliveryTypeEnum;
import com.jinlia.show.modules.base.constant.EnableStatusEnum;
import com.jinlia.show.modules.base.wxapp.entity.AppConfig;
import com.jinlia.show.modules.base.wxapp.entity.ShoppingCart;
import com.jinlia.show.modules.base.model.po.ShoppingCartPO;
import com.jinlia.show.modules.base.model.query.ShoppingCartQuery;
import com.jinlia.show.modules.base.wxapp.service.ShoppingCartMapperService;
import com.jinlia.show.modules.base.wxapp.service.UserAddressMapperService;
import com.jinlia.show.modules.wxapp.model.form.PreOrderForm;
import com.jinlia.show.modules.wxapp.model.vo.OrderVO;
import com.jinlia.show.modules.wxapp.model.vo.PreOrderVO;
import com.jinlia.show.modules.wxapp.model.vo.ShoppingCartVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Slf4j
@Service
@RequiredArgsConstructor
public class ShoppingCartService {
    @Autowired
    private ShoppingCartMapperService shoppingCartMapperService;
    @Autowired
    private UserAddressMapperService userAddressMapperService;
    @Autowired
    private AppConfig appConfig;

    /**
     * 查询购物车
     *
     * @param shoppingCartQuery
     * @return
     */
    public List<ShoppingCartVO> getVO(ShoppingCartQuery shoppingCartQuery) {
        List<ShoppingCartPO> shoppingCart = shoppingCartMapperService
                .getShoppingCart(shoppingCartQuery);
        List<ShoppingCartVO> collect = shoppingCart.stream().map(ShoppingCartVO::fromEntity).collect(Collectors.toList());
        return collect;
    }


    /**
     * 计算购物车的价格
     *
     * @param preOrderForm
     * @return
     */
    public OrderVO calculateMoney(PreOrderForm preOrderForm) {
        OrderVO orderVO = new OrderVO();
        if (CollectionUtils.isEmpty(preOrderForm.getShoppingCartIds())) {
            return orderVO;
        }
        //查询购物车信息
        List<ShoppingCart> list = shoppingCartMapperService.lambdaQuery()
                .in(ShoppingCart::getId, preOrderForm.getShoppingCartIds())
                .list();
        //步骤一:计算商品总价==>订单商品价
        BigDecimal originalMoney = list.stream()
                .map(ShoppingCart::getOriginalMoney)//map出原价
                .reduce(BigDecimal.ZERO, BigDecimal::add);//计算总价
        orderVO.setOriginalMoney(originalMoney);//设置原价
        orderVO.setGoodsSumMoney(originalMoney);
        //步骤三:计算折扣价()
        BigDecimal discountRate = BigDecimal.ONE;
        if (Objects.nonNull(appConfig.getDefaultDiscount())){
            discountRate = appConfig.getDefaultDiscount();
            log.info("公司默认折扣({})生效",discountRate);
        }

        //步骤四:预计算应收 = 原价(originalMoney) - 折扣价(discountMoney)
        orderVO.setReceivableMoney(orderVO.getOriginalMoney().subtract(orderVO.getDiscountMoney()));
        //步骤五:计算抹零
        //计算应收向下取整的价格
        BigDecimal downReceivableMoney = orderVO.getReceivableMoney().setScale(1, RoundingMode.DOWN);
        //设置抹零价格
        if (orderVO.getReceivableMoney().compareTo(downReceivableMoney) == 0) {
            //无需抹零
            orderVO.setMlMoney(BigDecimal.ZERO);
        } else {
            //向下取整的价格与原价不一致 ==>无需抹零
            orderVO.setMlMoney(orderVO.getReceivableMoney().subtract(downReceivableMoney));
        }
        //步骤五：计算商品总价应收 = 原价(originalMoney) - 折扣价(discountMoney)- 抹零(mlMoney)
        orderVO.setReceivableMoney(orderVO.getOriginalMoney().subtract(orderVO.getDiscountMoney()).subtract(orderVO.getMlMoney()));
        //计算费用 ==>订单总价
        if (DeliveryTypeEnum.OTHER_PICT.getValue().equals(preOrderForm.getDeliveryType())) {
            //外卖订单
            orderVO.setDeliveryMoney(appConfig.getDefaultFreight());//运费
            //重新设置原价
            orderVO.setOriginalMoney(orderVO.getOriginalMoney().add(orderVO.getDeliveryMoney()));
            //重新设置应收
            orderVO.setReceivableMoney(orderVO.getReceivableMoney().add(orderVO.getDeliveryMoney()));
        }
        return orderVO;
    }

    /**
     * 删除购物车
     *
     * @param shoppingCartIds
     * @return
     */
    public Boolean shoppingCartService(List<Long> shoppingCartIds) {
        return shoppingCartMapperService.removeBatchByIds(shoppingCartIds);
    }

    /**
     * 预下单
     *
     * @param preOrderForm
     * @return
     */
    public PreOrderVO orderPre(PreOrderForm preOrderForm) {
        PreOrderVO preOrderVO = new PreOrderVO();
        preOrderVO.setOrderVO(calculateMoney(preOrderForm));
        //设置购物车
        ShoppingCartQuery shoppingCartQuery = new ShoppingCartQuery();
        shoppingCartQuery.setIds(preOrderForm.getShoppingCartIds());
        shoppingCartQuery.setEnableStatus(EnableStatusEnum.ENABLE.getValue());
        List<ShoppingCartPO> shoppingCart = shoppingCartMapperService
                .getShoppingCart(shoppingCartQuery);
//        preOrderVO.getOrderVO().setShoppingCartPOs(shoppingCart);
        if (!CollectionUtils.isEmpty(preOrderForm.getCouponIds())) {
            //保函优惠券
        }

        //用户地址
        preOrderVO.setUserAddressList(userAddressMapperService.list());

        return preOrderVO;
    }
}
